package com.beiben.experiment.equipment.service.impl;

import java.util.Date;
import java.util.List;

import com.beiben.common.GlobalConstant;
import com.beiben.experiment.repair.domain.Repair;
import com.beiben.experiment.repair.mapper.RepairMapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beiben.experiment.equipment.mapper.EquipmentMapper;
import com.beiben.experiment.equipment.domain.Equipment;
import com.beiben.experiment.equipment.service.IEquipmentService;

/**
 * 试验设备\计量设备信息Service业务层处理
 * 
 * @author 熊国强
 * @date 2020-11-11
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService 
{

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private RepairMapper repairMapper;



    /**
     * 查询试验设备\计量设备信息
     * 
     * @param equipmentId 试验设备\计量设备信息ID
     * @return 试验设备\计量设备信息
     */
    @Override
    public Equipment selectEquipmentById(Long equipmentId)
    {
        return equipmentMapper.selectEquipmentById(equipmentId);
    }

    /**
     * 查询试验设备\计量设备信息列表
     * 
     * @param equipment 试验设备\计量设备信息
     * @return 试验设备\计量设备信息
     */
    @Override
    public List<Equipment> selectEquipmentList(Equipment equipment)
    {
        return equipmentMapper.selectEquipmentList(equipment);
    }

    /**
     * 新增试验设备\计量设备信息
     * 
     * @param equipment 试验设备\计量设备信息
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment)
    {
        return equipmentMapper.insertEquipment(equipment);
    }

    /**
     * 修改试验设备\计量设备信息
     * 
     * @param equipment 试验设备\计量设备信息
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment)
    {
        return equipmentMapper.updateEquipment(equipment);
    }

    /**
     * 批量删除试验设备\计量设备信息
     * 
     * @param equipmentIds 需要删除的试验设备\计量设备信息ID
     * @return 结果
     */
    @Override
    public int deleteEquipmentByIds(Long[] equipmentIds)
    {
        repairMapper.deleteRepairByRelIds(equipmentIds,GlobalConstant.EXPERIMENT_EQUIPMENT);
        return equipmentMapper.deleteEquipmentByIds(equipmentIds);
    }

    /**
     * 删除试验设备\计量设备信息信息
     * 
     * @param equipmentId 试验设备\计量设备信息ID
     * @return 结果
     */
    @Override
    public int deleteEquipmentById(Long equipmentId)
    {
        return equipmentMapper.deleteEquipmentById(equipmentId);
    }

    /**
     * 导入数据
     *
     * @param list 数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param
     * @return 结果
     */
    @Override
    public String importEquipment(List<Equipment> list, Boolean isUpdateSupport) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Equipment equipment : list)
        {
            try
            {
                // 验证是否存在这个用户
                Equipment e = equipmentMapper.selectEquipmentByCode(equipment.getCode());
                if (StringUtils.isNull(e))
                {
                    this.insertEquipment(equipment);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号： " + equipment.getCode() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    equipment.setEquipmentId(e.getEquipmentId());
                    this.updateEquipment(equipment);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编号: " + equipment.getCode() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、编号: " + equipment.getCode() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编号: " + equipment.getCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共: " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共: " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 修改试验设备\计量设备信息状态
     *
     * @param equipmentIds 试验设备\计量设备信息IDS
     * @param state 使用状态
     * @return 结果
     */
    @Override
    public int updateEquipmentState(Long[] equipmentIds, String state) {
        if("0".equals(state)){
            Repair repair = new Repair();
            repair.setRelIds(equipmentIds);
            repair.setTableName(GlobalConstant.EXPERIMENT_EQUIPMENT);
            repair.setRelFinishTime(new Date());
            repair.setIsFinish("Y");
            repairMapper.updateRepairIsFinish(repair);
        }
        return equipmentMapper.updateEquipmentState(equipmentIds,state);
    }
}
